package com.smr.springboot.resttest.controller;

import com.smr.springboot.resttest.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController("/users")
public class TestController {
    private static final List<User> USERS = new ArrayList<>();

    /**
     * SELECT
     */
    @GetMapping
    public List<User> getUser(){
        return USERS;
    }

    /**
     * SELECT BY USERNAME
     */
    @GetMapping("/{username}")
    public User getUserByUserName(@PathVariable String username) {
        User user = null;
        for (User tempUser : USERS) {
            if (tempUser.getUsername().equals(username)){
                user = tempUser;
            }
        }
        return user;
    }

    /**
     * INSERT
     */
    @PostMapping
    public void postUser(@RequestBody User user){
        if (user.getUsername() == null){
            throw new RuntimeException("用户名不能为空");
        }
        USERS.add(user);
    }

    /**
     * DELETE
     */
    @DeleteMapping
    public void deleteUser(User user){
        USERS.remove(user);
    }
}
